package com.community.web.controller.community;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;


import com.community.body.domain.ZyBuilding;
import com.community.body.domain.ZyCommunity;
import com.community.body.service.IZyBuildingService;
import com.community.common.core.domain.entity.SysUser;
import com.community.common.core.domain.model.LoginUser;
import com.community.common.utils.ServletUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.community.common.annotation.Log;
import com.community.common.core.controller.BaseController;
import com.community.common.core.domain.AjaxResult;
import com.community.common.enums.BusinessType;

import com.community.common.utils.poi.ExcelUtil;
import com.community.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 楼栋信息Controller
 *
 * @author community
 * @date 2024-04-09
 */
@RestController
@RequestMapping("/community/buildings")
public class ZyBuildingController extends BaseController {
    @Autowired
    private IZyBuildingService zyBuildingService;

    /**
     * 查询楼栋信息列表
     */
    @PreAuthorize("@ss.hasPermi('community:buildings:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZyBuilding zyBuilding) {
        startPage();
        List<ZyBuilding> list = zyBuildingService.selectZyBuildingList(zyBuilding);
        return getDataTable(list);
    }

    /**
     * 导出楼栋信息列表
     */
    @PreAuthorize("@ss.hasPermi('community:buildings:export')")
    @Log(title = "楼栋信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZyBuilding zyBuilding) {
        String[] split = zyBuilding.getBuildingCode().split(",");
        Long[] longArray = Arrays.stream(split).map(Long::valueOf).toArray(Long[]::new);
        List<ZyBuilding> list = zyBuildingService.selectZyBuildingExport(longArray);
//        List<ZyBuilding> list = zyBuildingService.selectZyBuildingList(zyBuilding);
        ExcelUtil<ZyBuilding> util = new ExcelUtil<ZyBuilding>(ZyBuilding.class);
        util.exportExcel(response, list, "楼栋信息数据");
    }

    /**
     * 导入楼栋信息
     */
    @PreAuthorize("@ss.hasPermi('community:buildings:importData')")
    @Log(title = "楼栋管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws IOException {
        ExcelUtil<ZyBuilding> util = new ExcelUtil<>(ZyBuilding.class);
        List<ZyBuilding> buildingList = util.importExcel(file.getInputStream());

        String message = zyBuildingService.importData(buildingList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 下载模板
     *
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ZyBuilding> util = new ExcelUtil<ZyBuilding>(ZyBuilding.class);
        util.importTemplateExcel(response, "楼栋信息导入模板");
    }


    /**
     * 获取楼栋信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('community:buildings:query')")
    @GetMapping(value = "/{buildingId}")
    public AjaxResult getInfo(@PathVariable("buildingId") Long buildingId) {
        return success(zyBuildingService.selectZyBuildingByBuildingId(buildingId));
    }

    /**
     * 新增楼栋信息
     */
    @PreAuthorize("@ss.hasPermi('community:buildings:add')")
    @Log(title = "楼栋信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZyBuilding zyBuilding) {
        return toAjax(zyBuildingService.insertZyBuilding(zyBuilding));
    }

    /**
     * 修改楼栋信息
     */
    @PreAuthorize("@ss.hasPermi('community:buildings:edit')")
    @Log(title = "楼栋信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZyBuilding zyBuilding) {
        return toAjax(zyBuildingService.updateZyBuilding(zyBuilding));
    }

    /**
     * 删除楼栋信息
     */
    @PreAuthorize("@ss.hasPermi('community:buildings:remove')")
    @Log(title = "楼栋信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{buildingIds}")
    public AjaxResult remove(@PathVariable Long[] buildingIds) {
        return toAjax(zyBuildingService.deleteZyBuildingByBuildingIds(buildingIds));
    }
}
